<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicTableCols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          v-if="scope.row.AREA_TYPE==='00'"
          type="primary"
          class="el-icon-edit"
          :underline="false"
          @click="edit(scope.row.index)"
          style="font-size:12px; margin-right:10px;"
        >编辑</el-link>
        <!-- <el-link
          type="primary"
          class="el-icon-printer"
          :underline="false"
          @click="print(scope.row)"
          style="font-size:12px;"
        >打印库位标签</el-link> -->
      </template>
    </one-table-template>
    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :is-show-chooes="isShowChooes"
      :warehouseArr="warehouseArr"
      @close="close"
    />
    <!-- <print @colse="colsePrint" :data="printData" v-if="isShow"></print> -->
    <print v-if="isShow" :rowData="rowData" @detailClose="detailClose" :isShow="isShow" />
  </div>
</template>

<script>
import OneTableTemplate from "@/components/wms/templates/oneTable";
import { basedataApi } from "@/api/wms/basedata";
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import requesta from "../../../../utils/request.js";
export default {
  components: {
    OneTableTemplate,
    Edit: () => import("./edit"),
    Print: () => import("./print")
  },
  mixins: [oneTableWithViewTemplateMixins],
  data() {
    return {
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          name: "search",
          position: "right",
          clickEvent: () => this.queryTable(1),
          text: this.$t("sys.button.query") /*查询*/
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: this.$t("sys.button.reset") /*重置*/
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "add ",
          position: "right",
          clickEvent: () => this.add(),
          text: "新增" /*导出*/
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          name: "add ",
          position: "right",
          clickEvent: () => this.export(),
          text: "导出" /*导出*/
        },
        {
          compKey: "btnKey5",
          text: "导入",
          category: "ve",
          codeField: "fileChannel",
          position: "upload",
          isMust: true
        }, //导入
        {
          compKey: "btnKey6",
          type: "",
          size: "small",
          name: "importTemplateDownload",
          position: "right",
          clickEvent: () => this.downloadTemplate(),
          text: "模板下载" //"模板下载"
        }
      ],
      tableComponents: [
        {
          compKey: "compKey2",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/org/dropdownList/dropdownList"),
          options: [],
          type: "dropdownList",
          isMust: true,
          isMul: true
        },
        {
          compKey: "compKey1",
          labelName: "库区编号",
          codeField: "AREA_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey3",
          labelName: "库区名称",
          codeField: "AREA_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "库位编号",
          codeField: "PLACE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        }
      ],
      tableCols: this.getCols(),
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        WAREHOUSE_IDS_NEW: "",
        AREA_CODE: "",
        AREA_NAME: "",
        PLACE_CODE: ""
      },
      isShowChooes: false,
      apiConfig: basedataApi.queryplace,
      warehouseArr: [],
      isShow: false,
      rowData: {},
      // 控制初始化页面
      initTemplate: false
    };
  },
  created() {
    this.dropdownList();
  },
  mounted() {},
  methods: {
    add() {
      // this.isShowChooes = true;
      this.showEdit("add");
    },
    print(row) {
      this.rowData = row;
      this.isShow = true;
    },
    detailClose() {
      this.isShow = false;
    },
    dropdownList() {
      requesta({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "POST",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        data: {}
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.formField.WAREHOUSE_IDS_NEW += item.WAREHOUSE_ID + ",";
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.warehouseArr = this.tableComponents[0].options;
          this.formField.WAREHOUSE_IDS = this.warehouseArr[0].value;
          this.formField.WAREHOUSE_IDS_NEW = this.formField.WAREHOUSE_IDS_NEW.substr(
            0,
            this.formField.WAREHOUSE_IDS_NEW.length - 1
          );
          this.formField.WAREHOUSE_IDS = res.listRow[0].WAREHOUSE_ID;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      requesta({
        url: "/mp/wm/vl/basedata/place/exportplace.do",
        method: "POST",
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "库位管理.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.info(err);
        });
    },
    downloadTemplate() {
      let downloadElement = document.createElement("a");
      let href = "/wms/fileTemplate/库位管理模板.xlsx"; // 创建下载的链接
      downloadElement.href = href;
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
    },
    handFileListChange(file, fileList) {},
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      if (file.size > 50 * 1024 * 1024) {
        this.$message({
          message: "文件大小不能超过50M",
          type: "warning"
        });
        return false;
      }
      let fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message({
          message: "只能导入后缀为xls,xlsx的文件！",
          type: "warning"
        });
        return false;
      }
      return true;
    },
    handleChange(param) {
      const loading = this.$loading({
        lock: true,
        text: "导入中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const that = this;
      const url = "/mp/wm/vl/basedata/place/importExcelToPlace.do";
      that
        .$requestUpload(url, param)
        .then(response => {
          loading.close();
          if (response.result === "1") {
            this.$message({
              message: "导入成功",
              type: "success"
            });
            that.queryTable();
          } else {
            this.$message({
              message: response.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {});
    },
    getCols() {
      var cols = [
        {
          prop: "controlBtn",
          label: this.$t("sys.content.operate") /*操作*/,
          codeField: "controlBtn",
          width: 150,
          align: "center",
          isSlot: true
        }
      ];
      cols = cols.concat([
        {
          prop: "WAREHOUSE_CODE",
          label: "仓库编码",
          align: "center"
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          align: "center"
        },
        {
          prop: "AREA_CODE",
          label: "库区编号",
          align: "center"
        },
        {
          prop: "AREA_NAME",
          label: "库区名称",
          width: "150",
          align: "center"
        },
        {
          prop: "PLACE_CODE",
          label: "库位编号",
          align: "center"
        },
        {
          prop: "IS_PICK_STR",
          label: "可拣货标识",
          align: "center"
        },
        {
          prop: "IS_PUTON_STR",
          label: "可上架标识",
          align: "center"
        },
        {
          prop: "IS_MIX_STR",
          label: "可混装标识",
          align: "center"
        },
        {
          prop: "IS_INFINITY_STR",
          label: "体积无限标识",
          align: "center"
        },
        {
          prop: "CREATED_DATE",
          label: "创建时间",
          width: "150",
          align: "center"
        },
        {
          prop: "CREATOR",
          label: "创建人",
          align: "center"
        },
        {
          prop: "IS_ENABLE_STR",
          label: "状态",
          align: "center"
        }
      ]);
      return cols;
    }
  }
};
</script>

